﻿using Microsoft.Extensions.Configuration;
using System.Globalization;
using System.Reflection;

namespace MiniFox.Configurations
{
    /// <summary>
    /// 
    /// </summary>
    public class ProviderSettings
    {
        const string TYPE = "type";
        const string NAME = "name";
        /// <summary>
        /// 
        /// </summary>
        [ConfigurationKeyName(NAME)]
        public string Name { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [ConfigurationKeyName("displayName")]
        public string DisplayName { get; set; } = null;
        /// <summary>
        /// 提供者类型
        /// </summary>
        [ConfigurationKeyName(TYPE)]
        public string TypeName { get; set; }
        internal Type Type
        {
            get
            {
                return Type.GetType(TypeName);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="providerSettings"></param>
        /// <returns></returns>
        public static implicit operator Type(ProviderSettings providerSettings)
        {
            if (providerSettings == null)
                return null;

            return providerSettings.Type;
        }

        /// <summary>
        /// 初始化参数列表及默认值
        /// </summary>
        [ConfigurationKeyName("parameters")]
        public Dictionary<string, string> Parameters { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public object CreateInstance(params object[] args)
        {
            return Activator.CreateInstance(this.Type, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, System.Type.DefaultBinder, args, CultureInfo.CurrentCulture);
        }
    }
}
